package com.xc.ezplan.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xc.ezplan.admin.entity.AdminQiniuContent;
import com.xc.ezplan.admin.service.AdminQiniuContentService;
import com.xc.ezplan.admin.vo.AdminQiniuContentVo;
import com.xc.ezplan.core.base.BaseSearchBody;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author lixingchen
 * @date 2022/11/21
 * @description 七牛云存储
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/qiNiu/content")
@Api(tags = "七牛云存储")
public class AdminQiNiuContentController {

    private final AdminQiniuContentService adminQiniuContentService;

    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportQiNiu(HttpServletResponse response, AdminQiniuContentVo adminQiniuContentVo) throws IOException {
        adminQiniuContentService.exportQiNiu(response,adminQiniuContentVo);
    }

    @ApiOperation("按条件分页查询文件")
    @PostMapping("/page")
    public EzResult<Object> pageQueryQiNiu(@RequestBody BaseSearchBody<AdminQiniuContent> baseSearchBody){
        return EzResult.ok(adminQiniuContentService.pageQueryQiNiu(baseSearchBody));
    }

    @Log("上传文件")
    @ApiOperation("上传文件")
    @PostMapping
    @SaCheckPermission("qiniu.upload")
    public EzResult<Object> uploadQiNiu(@RequestBody MultipartFile file,@RequestParam Long configId){
        return EzResult.ok(adminQiniuContentService.uploadQiNiu(file,configId));
    }

    @Log("同步七牛云数据")
    @ApiOperation("同步七牛云数据")
    @GetMapping(value = "/synchronize")
    public EzResult<Object> synchronizeQiNiu(@RequestParam Long configId){
        adminQiniuContentService.synchronizeQiNiu(configId);
        return EzResult.ok();
    }

    @Log("下载文件")
    @ApiOperation("下载文件")
    @GetMapping("/download/{id}")
    @SaCheckPermission("qiniu.download")
    public EzResult<Object> downloadQiNiu(@PathVariable Long id){
        return EzResult.ok(adminQiniuContentService.downloadQiNiu(id));
    }

    @Log("删除文件")
    @ApiOperation("删除文件")
    @DeleteMapping
    @SaCheckPermission("qiniu.delete")
    public EzResult<Object> deleteQiNiu(@RequestBody List<Long> ids, @RequestParam Long configId){
        adminQiniuContentService.deleteQiNiu(ids,configId);
        return EzResult.ok();
    }

}
